package com.example.zzm.yuejian03.Activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.SearchView;
import android.text.TextUtils;
import android.util.Log;

import com.example.zzm.yuejian03.Adapter.sousuoAdapter;
import com.example.zzm.yuejian03.Bean.SousuoBean;
import com.example.zzm.yuejian03.Listener.SouListener;
import com.example.zzm.yuejian03.Model.SouModel;
import com.example.zzm.yuejian03.R;

import java.util.ArrayList;
import java.util.List;

public class sousuoActivitiy extends AppCompatActivity {

    private SearchView searchView;
    private RecyclerView recyclerView;
    private ArrayList<SousuoBean> mSearch;
    private sousuoAdapter sousuoAdapter;

    public void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sousuo);
        initView();
        initEvent();
        SouModel searchModel = new SouModel();
        searchModel.getSou(listener);
    }

    SouListener listener = new SouListener() {
        @Override
        public void onResponse(List<SousuoBean> list) {
            mSearch = new ArrayList<>();
            mSearch = (ArrayList<SousuoBean>) list;

        }

        @Override
        public void onFail(String msg) {

        }
    };
    private void initView() {
        searchView = (SearchView)findViewById(R.id.search) ;
        searchView.setFocusable(false);
        searchView.clearFocus();

        recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
    }

    private void initEvent() {
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            // 当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)){
                    sousuoAdapter = new sousuoAdapter(getApplicationContext(), filter(mSearch,newText));
                    recyclerView.setAdapter(sousuoAdapter);
                    //Toast.makeText(SearchActivity.this,Integer.toString(filter(mSearch,newText).size()), Toast.LENGTH_SHORT).show();
                }else{
                    List<SousuoBean> list = new ArrayList<>();
                    sousuoAdapter = new sousuoAdapter(getApplicationContext(),list);
                    recyclerView.setAdapter(sousuoAdapter);
                }
                return false;
            }
        });
    }
    private ArrayList<SousuoBean> filter(ArrayList<SousuoBean> strings,String text) {
        ArrayList<SousuoBean> filterString=new ArrayList<>();
        Log.i("mSearch的size:",Integer.toString(strings.size()));
        for (int i = 0; i < strings.size(); i++) {
            if (strings.get(i).getFlag()==1) {
                if (strings.get(i).getClub_name().contains(text)) {
                    filterString.add(strings.get(i));
                }
            }
            if (strings.get(i).getFlag()==2) {
                if (strings.get(i).getTrain_name().contains(text)) {
                    filterString.add(strings.get(i));
                }
            }

        }
        return filterString; }

}
